package com.good.jzhouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.good.jzhouse.bean.Img;
import com.good.jzhouse.dao.ImgDao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Random;

@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    ImgDao imgDao;



    @RequestMapping("/fileUpdate")
    @ResponseBody
    public String userProfileUpdate(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        JSONObject res = new JSONObject();
        JSONObject resUrl = new JSONObject();
        //if(UserAuth.authUser(request))
      //  {
           // String username=UserAuth.getUsername(request);
            //上传文件路径
            String path =request.getSession().getServletContext().getRealPath("imgs")+"\\";

            System.out.println("文件名称"+file.getOriginalFilename());
            //上传文件名
            String name = file.getOriginalFilename();//上传文件的真实名称
            String suffixName = name.substring(name.lastIndexOf("."));//获取后缀名
            String hash = Integer.toHexString(new Random().nextInt());//自定义随机数（字母+数字）作为文件名
            String fileName = hash + suffixName;
            HashMap<String,String> profile=new HashMap<String,String>();
//         userService
           // profile.put("username", username);
            profile.put("profile", fileName);
            /*boolean userProfileUpdate = userService.userProfileUpdate(profile);*/
            boolean userProfileUpdate = imgDao.insert(new Img(null,fileName));
            if(userProfileUpdate)
            {
                File filepath = new File(path, fileName);
                System.out.println("文件地址："+path);
                System.out.println("文件地址："+fileName);
                // System.out.println("随机数文件名称"+filepath.getName());
                //判断路径是否存在，没有就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文档中
                File tempFile = new File(path + fileName);
                file.transferTo(tempFile);

                resUrl.put("src", tempFile.getPath());
                res.put("code", 0);
                res.put("msg", "上传成功！");
                res.put("data", resUrl);
                //str = "{\"code\": 0,\"msg\": \"上传成功\",\"data\": {\"src\":\""+path+fileName + "\"}}";
                // System.out.println("res里面的值：");
                System.out.println(res.toString());
                return res.toString();
            }
            else
            {
                res.put("code", 0);
                res.put("msg", "上传失败！");
                res.put("data", null);
                return res.toString();
            }
        /*}
        else
        {
            res.put("code", 0);
            res.put("msg", "上传失败！");
            res.put("data", null);
            return res.toString();
        }*/
    }

}
